<?php
namespace Foam\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;

class ProduitAllergeneTable extends AbstractTableGateway
{
    protected $table ='foam_produit_allergene';
    
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new ProduitAllergene());
        $this->initialize();
    }
    
    public function fetchAll()
    {
        $resultSet = $this->select();
        return $resultSet;
    }
    
    public function getProduitAllergene($id_produit)
    {
        $id_produit  = (int) $id_produit;
        $adapter = $this->adapter;
        $sql = new Sql($adapter);
        $select = $sql->select();
        $select->columns(array('id_allergene'));
        $select->from($this->table);
        
        $where = new Where();
        $where->equalTo('id_produit', $id_produit);
        $select->where($where);
        
        $selectString = $sql->getSqlStringForSqlObject($select);
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);   
        return $results;
    }
    
    public function addProduitAllergene($id_prd_alg,$id_allergene,$id_produit)
    {
        $data = array(
            'id_prd_alg' => $id_prd_alg,
            'id_allergene' => $id_allergene,
            'id_produit' => $id_produit,
        );
        
        $this->insert($data);
    }
    
    public function deleteProduitAllergene($id_produit)
    {
        $this->delete(array('id_produit' => $id_produit));
    }
}
